package com.example.xuecheng.contentservice.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @description 跨域过虑器
 * @author Mr.M
 * @date 2022/9/7 11:04
 * @version 1.0
 */
 @Configuration
 public class GlobalCorsConfig {


  /**
   *    允许跨域调用的过滤器
   */
  @Bean
  public CorsFilter corsFilter(){
   CorsConfiguration corsConfiguration = new CorsConfiguration();
   //允许白名单域名进行跨域调用
   corsConfiguration.addAllowedOrigin("*");
   //允许跨域发送cookie
   corsConfiguration.setAllowCredentials(true);
   //放行全部原始头信息
   corsConfiguration.addAllowedHeader("*");
   //允许所有请求方法跨域调用
   corsConfiguration.addAllowedMethod("*");
   UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
   urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
   return new CorsFilter(urlBasedCorsConfigurationSource);
  }
 }
